﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Xml.Serialization;

namespace OrderControll
{
    public class OrderDetails
    {
        private string _product;
        private double _cost;
        public OrderDetails(string product,double cost)
        {
            Product = product;
            Cost = cost;
        }
        public OrderDetails()
        {

        }

        public string Product
        {
            get => _product;
            set => _product = value;
        }
        public double Cost
        {
            get => _cost;
            set
            {
                _cost = value;
            }
        }

        public double getCost()
        {
            return Cost;
        }
        public override string ToString()
        {
            return Product + "\t" + Cost + "\t" + getCost();
        }
    }
    public class Order
    {
        int _id;
        string _customer;
        OrderDetails _orderdetails;
        public Order(int id, string customer, OrderDetails orderdetail)
        {
            Id = id;
            Customer = customer;
            Orderlists = orderdetail;
        }
        public Order()
        {

        }
        public int Id { get => _id; set => _id = value; }
        public string Customer { get => _customer; set => _customer = value; }
        public OrderDetails Orderlists { get => _orderdetails; set => _orderdetails = value; }

        public override string ToString()
        {
            return Id + "\t" + Customer + "\t" + Orderlists.ToString();
        }
        public override bool Equals(Object od)
        {
            return this.Id == ((Order)od).Id;
        }
    }
    public class OrderService
    {
        List<Order> orders;

        public OrderService()
        {
            orders = new List<Order>();
        }

        public void showMenu()
        {
            Console.WriteLine("欢迎使用订单管理系统");
            Console.WriteLine("1.添加订单");
            Console.WriteLine("2.删除订单");
            Console.WriteLine("3.修改订单");
            Console.WriteLine("4.查看订单");
            Console.WriteLine("5.查询订单");
            Console.WriteLine("6.序列化订单为xml");
            Console.WriteLine("7.反序列化xml");
            Console.WriteLine("0.退出系统");
            Console.WriteLine("请输入想要执行的任务:");

        }
        public void modifyOrder()
        {
            try
            {
                Console.WriteLine("请输入要修改订单编号:");
                int id1 = Convert.ToInt32(Console.ReadLine());
                var ods = from od in orders
                          where od.Id == id1
                          select od;
                if (ods.Count() == 0)
                {
                    Console.WriteLine("该订单不存在！\nPress Enter Key to Continue.......");
                    return;
                }
                Order t = ods.First();
                int id;
                string customer, product;
                double cost;
                bool flag = true;
                Console.WriteLine("请输入修改后订单编号:");
                id = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入修改后客户名称:");
                customer = Console.ReadLine();
                Console.WriteLine("请输入修改后商品名称:");
                product = Console.ReadLine();
                Console.WriteLine("请输入修改后商品价格:");
                cost = Convert.ToDouble(Console.ReadLine());
                Order o = new Order(id, customer, new OrderDetails(product,cost));
                orders.ForEach(delegate (Order od)
                {
                    if (o.Equals(od)) flag = false;
                });
                if (!flag && id != id1)
                {
                    Console.WriteLine("该订单编号已存在");
                    return;
                }
                t.Id = o.Id;
                t.Customer = o.Customer;
                t.Orderlists = o.Orderlists;
                Console.WriteLine("修改成功\nPress Enter Key to Continue.......");
            }
            catch
            {
                Console.WriteLine("输入错误");
            }
        }
        public void deleteOrder()
        {
            int id = 0;
            IEnumerable<Order> ods = null;
            try
            {
                Console.WriteLine("请输入要删除订单编号:");
                id = Convert.ToInt32(Console.ReadLine());
                ods = from odr in orders
                      where odr.Id == id
                      select odr;
                if (ods.Count() == 0)
                {
                    Console.WriteLine("该订单不存在！\nPress Enter Key to Continue.......");
                    return;
                }

            }
            catch
            {
                Console.WriteLine("该指令非法，请重新输入\nPress Enter Key to Continue.......");
                return;
            }
            Order od = ods.First();
            orders.Remove(od);
            Console.WriteLine("删除成功\nPress Enter Key to Continue.......");
        }
        public void showOrder()
        {
            if (orders.Count() == 0)
            {
                Console.WriteLine("暂无订单！\nPress Enter Key to Continue.......");
                return;
            }
            Console.WriteLine("\n查看订单");
            try
            {
                Console.WriteLine("请选择排序方式: (1: 按订单号排序  2:按订单金额排序)");

                int tmep = Convert.ToInt32(Console.ReadLine());
                IEnumerable<Order> ords = null;
                if (tmep == 1)
                {
                    ords = from order in orders
                           orderby order.Id ascending
                           select order;
                }
                else if (tmep == 2)
                {
                    ords = orders.OrderByDescending(o => o.Orderlists.getCost());
                }
                else throw new Exception();

                Console.WriteLine("订单号\t客户\t商品\t数量\t价格\n");
                foreach (var ord in ords) Console.WriteLine(ord);
                Console.WriteLine("Press Enter Key to Continue.......");
            }
            catch
            {
                Console.WriteLine("该指令非法，请重新输入\nPress Enter Key to Continue.......");
            }

        }
        public void AddOrder()
        {
            Console.WriteLine("\n添加订单:\n");
            try
            {
                int id;
                string customer, product;
                double cost;
                bool flag = true;
                Console.WriteLine("请输入订单编号:");
                id = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入客户姓名:");
                customer = Console.ReadLine();
                Console.WriteLine("请输入商品名称:");
                product = Console.ReadLine();
                Console.WriteLine("请输入商品价格:");
                cost = Convert.ToDouble(Console.ReadLine());
                Order o = new Order(id, customer, new OrderDetails(product,cost));
                orders.ForEach(delegate (Order order)
                {
                    if (o.Equals(order)) flag = false;
                    Console.WriteLine("该订单编号已存在\nPress Enter Key to Continue.......");
                });

                if (!flag)
                {
                    return;
                }
                orders.Add(o);
                Console.WriteLine("添加成功\nPress Enter Key to Continue.......");
            }
            catch
            {
                Console.WriteLine("该指令非法，请重新输入\nPress Enter Key to Continue.......");
            }
        }
        public void Export()
        {
            XmlSerializer xml = new XmlSerializer(typeof(Order[]));
            using (FileStream fs = new FileStream("s.xml", FileMode.Create)) 
            {
                xml.Serialize(fs,orders.ToArray());
            }
            Console.WriteLine("\nSerializer as xml:");
            Console.WriteLine(File.ReadAllText("s.xml"));
        }
        public void Import()
        {
            XmlSerializer xml = new XmlSerializer(typeof(Order[]));
            using (FileStream fs = new FileStream("s.xml", FileMode.Open))
            {
                Order[] orders = (Order[])xml.Deserialize(fs);
                Console.WriteLine("\nDeserialize from s.mxl");
                Array.ForEach(orders, (Order o) =>Console.WriteLine(o.ToString()));
            } 
        }
        public void queryOrder()
        {
            Console.WriteLine("请选择查找方式:");
            Console.WriteLine("1.按订单号查找");
            Console.WriteLine("2.按客户姓名查找");
            Console.WriteLine("3.按商品名称查找");
            Console.WriteLine("4.按订单金额查找");
            int select;
            try
            {
                select = Convert.ToInt32(Console.ReadLine());
                IEnumerable<Order> ords = null;
                switch (select)
                {
                    case 1:
                        Console.WriteLine("请输入查询的订单号:");
                        int id = Convert.ToInt32(Console.ReadLine());
                        ords = from order in orders
                               where order.Id == id
                               select order;
                        break;

                    case 2:
                        Console.WriteLine("请输入查询的客户名:");
                        string customer = Console.ReadLine();
                        ords = from order in orders
                               where order.Customer == customer
                               select order;
                        break;

                    case 3:
                        Console.WriteLine("请输入查询的商品名:");
                        string product = Console.ReadLine();
                        ords = from order in orders
                               where order.Orderlists.Product == product
                               select order;
                        break;

                    case 4:
                        Console.WriteLine("请输入查询的订单的金额:");
                        double cost = Convert.ToDouble(Console.ReadLine());
                        ords = from order in orders
                               where order.Orderlists.getCost() == cost
                               select order;
                        break;
                    default:
                        throw new Exception();
                }
                if (ords.Count() == 0)
                {
                    Console.WriteLine("无符合条件的订单\nPress Enter Key to Continue.......");
                    return;
                }
                ords = from od in ords
                       orderby od.Id ascending
                       select od;
                Console.WriteLine("订单号\t客户\t商品\t数量\t价格\n");
                foreach (var ord in ords) Console.WriteLine(ord);
            }
            catch
            {
                Console.WriteLine("该指令非法，请重新输入\nPress Enter Key to Continue.......");
            }


        }
    }
}

namespace OrderControll
{
    class Program
    {
        static void Main(string[] args)
        {
            OrderService osv = new OrderService();
            while (true)
            {
                osv.showMenu();
                int t;
                try
                {
                    t = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    t = -1;
                }
                switch (t)
                {
                    case 1:
                        osv.AddOrder();
                        break;
                    case 2:
                        osv.deleteOrder();
                        break;
                    case 3:
                        osv.modifyOrder();
                        break;
                    case 4:
                        osv.showOrder();
                        break;
                    case 5:
                        osv.queryOrder();
                        break;
                    case 6:
                        osv.Export();
                        break;
                    case 7:
                        osv.Import();
                        break;
                    case 0:
                        Console.WriteLine("程序已关闭！");
                        return;
                    default:
                        Console.WriteLine("输入错误,请重试");
                        break;
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
    }


}